package com.dci.service.impl.node;

import com.dci.dao.pms.node.NodeInfoMapper;
import com.dci.dao.pms.node.NodeMatterInfoMapper;
import com.dci.dao.pms.schedule.ProjectNodeInfoMapper;
import com.dci.model.node.NodeInfo;
import com.dci.model.node.NodeMatterInfo;
import com.dci.model.schedule.ProjectNodeInfo;
import com.dci.result.JsonData;
import com.dci.result.PageResult;
import com.dci.service.node.NodeInfoService;
import com.dci.util.UUIDBuild;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * All rights Reserved, Designed By 姑苏区政府大数据中心
 *
 * @version V1.0
 * @Title: null.java
 * @Package com.dci.service.impl.node
 * @Description: 项目节点实现类
 * @author: 张星亮
 * @date: 2019/2/25
 * @Copyright: 2019 姑苏区政府大数据中心. All rights reserved.
 * 注意：本内容仅限于姑苏区政府大数据中心内部传阅，禁止外泄以及用于其他的商业目
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class NodeInfoServiceImpl implements NodeInfoService {

    @Resource
    private NodeInfoMapper nodeInfoMapper;
    @Resource
    private ProjectNodeInfoMapper projectNodeInfoMapper;

    @Resource
    private NodeMatterInfoMapper nodeMatterInfoMapper;


    @Override
    public JsonData saveNodeInfo(NodeInfo record) {
        record.setId(UUIDBuild.getUUID());
        record.setCreateTime(new Date());
        nodeInfoMapper.insert(record);

        // 批量新增节点 事项信息
        if (record.getMatterList() != null) {
            List<NodeMatterInfo> list = record.getMatterList();
            list.forEach(matter -> {
                matter.setId(UUIDBuild.getUUID());
                matter.setNodeId(record.getId());
            });
            nodeMatterInfoMapper.batchInsert(list);
        }

        return JsonData.success(record, 906);
    }

    @Override
    public List<ProjectNodeInfo> selectByProjectId(ProjectNodeInfo record) {
        return projectNodeInfoMapper.selectByProjectId(record);
    }


    @Override
    public JsonData selectByPrimaryKey(NodeInfo record) {
        record = nodeInfoMapper.selectByPrimaryKey(record);
        return JsonData.success(record, 906);
    }

    @Override
    public JsonData updateByPrimaryKeySelective(NodeInfo record) {
        record.setLastEditTime(new Date());
        nodeInfoMapper.updateByPrimaryKeySelective(record);

        // 更新事项信息
        // 根据 nodeId 删除事项信息
        nodeMatterInfoMapper.deleteByNodeId(new NodeMatterInfo(record.getId()));

        // 新增事项信息
        List<NodeMatterInfo> list = record.getMatterList();
        list.forEach(matter -> {
            matter.setId(UUIDBuild.getUUID());
            matter.setNodeId(record.getId());
        });
        nodeMatterInfoMapper.batchInsert(list);

        return JsonData.success(record, 906);
    }

    @Override
    public PageResult selectPageList(NodeInfo record) {
        PageHelper.startPage(record.getPageNum(), record.getPageSize());
        List<NodeInfo> list = nodeInfoMapper.selectPageList(record);
        PageInfo<NodeInfo> pageInfo = new PageInfo<>(list);
        Long count = pageInfo.getTotal();
        return PageResult.success(list, null, null, count);
    }

    @Override
    public JsonData deleteNodeInfo(List<NodeInfo> list) {
        String del = "1";
        list.forEach(node -> node.setDelFlag(del));
        nodeInfoMapper.deleteNodeInfo(list);
        return JsonData.success(null, 906);
    }

    @Override
    public JsonData selectAll() {
        List<NodeInfo> list = nodeInfoMapper.selectAll();
        return JsonData.success(list, 906);
    }
}
